package com.example.calendar.util

import org.threeten.bp.LocalDate
import org.threeten.bp.LocalDateTime
import org.threeten.bp.LocalTime
import org.threeten.bp.format.DateTimeFormatter
import org.threeten.bp.temporal.ChronoUnit
import java.util.Locale

/**
 * Utility class for date and time related operations.
 */
object DateTimeUtils {
    
    // Common date formatters
    private val fullDateFormatter = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy", Locale.getDefault())
    private val mediumDateFormatter = DateTimeFormatter.ofPattern("MMM d, yyyy", Locale.getDefault())
    private val shortDateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy", Locale.getDefault())
    
    // Time formatters
    private val timeFormatter = DateTimeFormatter.ofPattern("h:mm a", Locale.getDefault())
    private val timeFormatterWithSeconds = DateTimeFormatter.ofPattern("h:mm:ss a", Locale.getDefault())
    
    // Date-time formatters
    private val dateTimeFormatter = DateTimeFormatter.ofPattern("MMM d, yyyy h:mm a", Locale.getDefault())
    
    /**
     * Format a LocalDate to a full date string (e.g., "Monday, January 1, 2023")
     */
    fun formatFullDate(date: LocalDate): String {
        return date.format(fullDateFormatter)
    }
    
    /**
     * Format a LocalDate to a medium date string (e.g., "Jan 1, 2023")
     */
    fun formatMediumDate(date: LocalDate): String {
        return date.format(mediumDateFormatter)
    }
    
    /**
     * Format a LocalDate to a short date string (e.g., "01/01/2023")
     */
    fun formatShortDate(date: LocalDate): String {
        return date.format(shortDateFormatter)
    }
    
    /**
     * Format a LocalTime to a time string (e.g., "2:30 PM")
     */
    fun formatTime(time: LocalTime): String {
        return time.format(timeFormatter)
    }
    
    /**
     * Format a LocalDateTime to a date-time string (e.g., "Jan 1, 2023 2:30 PM")
     */
    fun formatDateTime(dateTime: LocalDateTime): String {
        return dateTime.format(dateTimeFormatter)
    }
    
    /**
     * Calculate the duration between two LocalDateTime objects in minutes
     */
    fun durationInMinutes(start: LocalDateTime, end: LocalDateTime): Long {
        return ChronoUnit.MINUTES.between(start, end)
    }
    
    /**
     * Format a duration in minutes to a human-readable string
     */
    fun formatDuration(minutes: Long): String {
        val hours = minutes / 60
        val mins = minutes % 60
        
        return when {
            hours > 0 && mins > 0 -> "${hours}h ${mins}m"
            hours > 0 -> "${hours}h"
            else -> "${mins}m"
        }
    }
    
    /**
     * Check if a date is today
     */
    fun isToday(date: LocalDate): Boolean {
        return date.isEqual(LocalDate.now())
    }
    
    /**
     * Check if a date is yesterday
     */
    fun isYesterday(date: LocalDate): Boolean {
        return date.isEqual(LocalDate.now().minusDays(1))
    }
    
    /**
     * Check if a date is tomorrow
     */
    fun isTomorrow(date: LocalDate): Boolean {
        return date.isEqual(LocalDate.now().plusDays(1))
    }
    
    /**
     * Get a friendly representation of a date (e.g., "Today", "Tomorrow", "Yesterday", or formatted date)
     */
    fun getFriendlyDate(date: LocalDate): String {
        return when {
            isToday(date) -> "Today"
            isYesterday(date) -> "Yesterday"
            isTomorrow(date) -> "Tomorrow"
            else -> formatMediumDate(date)
        }
    }
} 